import {
  setPoint,
  getRootObj
} from './point/index.js'
let newUni = {}
export default {
  install(Vue, options) {
    // 注入组件
    Vue.mixin({
      data() {
        return {
          $$enterTime: '',
          $$outTime: '',
          $$currentPage: '',
          _newWx_: {}

        }
      },
      methods: {
        $$getTime() {
          return Date.parse(new Date())
        },
        $$startPoint(data){
          setPoint({pointType:'start',enterTime:Date.parse(new Date())})
        },
        getwxObj_({root,appVersion,param,api,success}){
          newUni = root
          getRootObj(root,appVersion,param,api,success)
        },
        $$eventPoint({type=0,posterType,desc={label:'',value:''}}){
          switch (type){
            case 0: // 其他事件类型 需要加描述
                setPoint({pointType:'event',enterTime:Date.parse(new Date()),shareType:type,posterType,desc})
              break;
            case 1: // type为1的时候是海报类型
                setPoint({pointType:'event',enterTime:Date.parse(new Date()),shareType:type,posterType})
              break;
            case 2: // type为2
                setPoint({pointType:'event',enterTime:Date.parse(new Date()),shareType:type})
              break;
            default:
              break;
          }
        },
        getRoute_(route) {
          newUni.setStorage({
            key: 'routeList',
            data: route
          });
        },
        $$upLoad() {
          if (this.$$currentPage) {
            newUni.getStorage({
              key: 'routeList',
              success: (res) => {
                if (res.data && res.data.includes(this.$$currentPage)) {
                  this.$$outTime = this.$$getTime()
                  setPoint('page', this.$$enterTime, this.$$outTime)
                }
              }
            });
          }
        }
      },
      onShow() {
        this.$$enterTime = this.$$getTime()
        let pages = getCurrentPages();
        let route = pages.length > 0 ? pages[pages.length - 1].route : ''
        this.$$currentPage = route
        // console.log(route)
        // if(!route){
        // 	this.$$startPoint()
        // }
      },
      onHide() {
        this.$$upLoad()

      },
      onUnload() {
        this.$$upLoad()
      },
    });
  }
}
